//
//  BKMatchingZhuanYeViewController.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/6/6.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKMatchingZhuanYeViewController.h"
#import "BKSearchField.h"
#import "BKButton.h"
#import "BKZhuanYeViewModel.h"
#import <RACEXTScope.h>
#import <ReactiveCocoa.h>
#import "BKResultOfZhuanYeViewController.h"
#import <MBProgressHUD/MBProgressHUD.h>

@interface BKMatchingZhuanYeViewController ()
@property (weak, nonatomic) IBOutlet BKSearchField *wenli;
@property (weak, nonatomic) IBOutlet BKSearchField *zhuanye;
@property (weak, nonatomic) IBOutlet BKSearchField *pici;
@property (weak, nonatomic) IBOutlet BKSearchField *xxzhuanye;
@property (weak, nonatomic) IBOutlet BKSearchField *kq;

@property (nonatomic, copy) NSArray *zhuanyeArray;
@property (nonatomic, copy) NSArray *xxzhuanyeArray;
@property (strong, nonatomic) BKZhuanYeViewModel *viewModel;
@end

@implementation BKMatchingZhuanYeViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{

    self= [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _viewModel = [[BKZhuanYeViewModel alloc] init];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _pici.dataArray = @[@"提前批",
                        @"本科一批",
                        @"本科二批",
                        @"本科三批",
                        @"高职专科"];
    _pici.flag = 8011;
    _wenli.dataArray = @[@"文科",@"理科"];
    _wenli.flag = 8011;

    @weakify(self);
    [RACObserve(self.pici, text) subscribeNext:^(id x) {
        @strongify(self);
        if ([x length] > 0) {
            NSString *piciStr = (NSString *)x;
            [BKZhuanYeViewModel zhuanyeTypeWithPici:piciStr success:^(NSArray *dataArray) {
                
                NSMutableArray *array = [NSMutableArray array];
                for (NSDictionary *dic in dataArray) {
                    if ([dic isKindOfClass:[NSDictionary class]]) {
                        if (dic[@"name"]) {
                            [array addObject:dic[@"name"]];
                        }
                    }
                }
                self.zhuanyeArray = dataArray;
                self.zhuanye.dataArray = array;
                self.zhuanye.flag = 8011;
            } failure:^(NSString *error) {
                
            }];
        }
    }];
    
    [RACObserve(self.zhuanye, text) subscribeNext:^(id x) {
        @strongify(self);
        if ([x length] > 0) {
            NSString *code = nil;
            for (NSDictionary *dict in self.zhuanyeArray) {
                
                if ([x isEqualToString:dict[@"name"]]) {
                    code = dict[@"code"];
                    break;
                }
            }
            
            [BKZhuanYeViewModel zhuanyeWithPiciCode:code success:^(NSArray *dataArray) {
                NSMutableArray *array = [NSMutableArray array];
                for (NSDictionary *dic in dataArray) {
                    if ([dic isKindOfClass:[NSDictionary class]]) {
                        if (dic[@"name"]) {
                            [array addObject:dic[@"name"]];
                        }
                    }
                }
                self.xxzhuanyeArray = dataArray;
                self.xxzhuanye.dataArray = array;
                self.xxzhuanye.flag = 8011;
            } failure:^(NSString *error) {
                
            }];
        }
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}
- (IBAction)p_submit:(id)sender {
    
    
    __weak BKMatchingZhuanYeViewController *weakSelf = self;

    
    if ([_xxzhuanye.text length] == 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择专业" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;

    }
    
    __block id code =@"";
    
    [_xxzhuanyeArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
       
        if ([obj isKindOfClass:[NSDictionary class]]) {
            if ([weakSelf.xxzhuanye.text isEqualToString:obj[@"name"]]) {
                code = obj[@"code"];
                *stop = YES;
            }
        }
    }];

    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = @"正在加载";
    [hud show:YES];
    UIButton *button = (UIButton *)sender;
    button.enabled = NO;
    [BKZhuanYeViewModel colleageWithKaoqu:_kq.text
                                    wenli:_wenli.text
                                 piciCode:code
                                    dalei:_zhuanye.text
                                  zhuanye:_xxzhuanye.text
                                    page:@"0"
                                  success:^(NSArray *dataArray)
     {
         [hud hide:YES];
         button.enabled = YES;
        BKResultOfZhuanYeViewController *result = [[BKResultOfZhuanYeViewController alloc] initWithNibName:NSStringFromClass([BKResultOfZhuanYeViewController class]) bundle:nil];
        result.zhuanye = weakSelf.xxzhuanye.text;
        result.Kaoqu = weakSelf.kq.text;
        result.wenli = weakSelf.wenli.text;
        result.piciCode = code;
        result.dalei = weakSelf.zhuanye.text;
        result.flag = dataArray.count == 20;
        [result.dataArray addObjectsFromArray:dataArray];
         //result.dataArray = [NSMutableArray array];
        [self.navigationController pushViewController:result animated:YES];
        
    } failure:^(NSString *message) {
        [hud hide:YES];
        button.enabled = YES;
    
    }];

}
- (IBAction)p_back:(id)sender {
     [self.navigationController popViewControllerAnimated:YES];
}


@end
